<div class="layui-fluid">
  <div class="layui-row">
    <div class="layui-col-xs12">
      <div class="layui-card">
        <div class="layui-card-header">
          <a href="#/layui/quanxianMana" title="返回上一页">
            <i class="layui-icon">&#xe65c;</i>
          </a>
          <span>编辑权限组</span>
        </div>
        <div class="layui-card-body">
          <form class="layui-form" action="" lay-filter="the_form">
            <div class="layui-form-item">
              <label class="layui-form-label">用户组名称<span class="red">*</span></label>
              <div class="layui-input-block">
                <input type="text" name="Name" required lay-verify="required" autocomplete="off" class="layui-input" id="user_id">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">用户组标志<span class="red">*</span></label>
              <div class="layui-input-block">
                <input type="text" name="ShortName" required lay-verify="required" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">控制页面<span class="red">*</span></label>
              <div class="layui-input-block page_area" style="height: 400px;overflow:auto;border:1px solid #999;padding: 5px;">
                <div class="min_title" data-value="0">基础数据</div>
                <div class="min_title" data-value="1">用户管理</div>
                <div class="min_title" data-value="2">商脉管理</div>
                <div class="min_title" data-value="3">产品管理</div>
                <div class="min_title" data-value="4">官网管理</div>
                <div class="min_title" data-value="5">系统设置</div>
<!--                 <input type="checkbox" name="" title="写作" lay-skin="primary">
                <input type="checkbox" name="" title="发呆" lay-skin="primary"> 
                <input type="checkbox" name="" title="禁用" lay-skin="primary">
                <input type="checkbox" name="" title="写作" lay-skin="primary">
                <input type="checkbox" name="" title="发呆" lay-skin="primary"> 
                <input type="checkbox" name="" title="禁用" lay-skin="primary"> 
                <input type="checkbox" name="" title="写作" lay-skin="primary">
                <input type="checkbox" name="" title="发呆" lay-skin="primary"> 
                <input type="checkbox" name="" title="禁用" lay-skin="primary"> 
                <input type="checkbox" name="" title="写作" lay-skin="primary">
                <input type="checkbox" name="" title="发呆" lay-skin="primary"> 
                <input type="checkbox" name="" title="禁用" lay-skin="primary"> 
                <input type="checkbox" name="" title="写作" lay-skin="primary">
                <input type="checkbox" name="" title="发呆" lay-skin="primary"> 
                <input type="checkbox" name="" title="禁用" lay-skin="primary"> 
                <input type="checkbox" name="" title="写作" lay-skin="primary">
                <input type="checkbox" name="" title="发呆" lay-skin="primary"> 
                <input type="checkbox" name="" title="禁用" lay-skin="primary">   -->
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">用户组用户<span class="red">*</span></label>
              <div class="layui-input-block user_area" style="height: 400px;overflow:auto;border:1px solid #999;padding: 5px;">
               <!--  <input type="checkbox" name="" title="写作" lay-skin="primary">
                <input type="checkbox" name="" title="发呆" lay-skin="primary"> 
                <input type="checkbox" name="" title="禁用" lay-skin="primary">
                <input type="checkbox" name="" title="写作" lay-skin="primary">
                <input type="checkbox" name="" title="发呆" lay-skin="primary"> 
                <input type="checkbox" name="" title="禁用" lay-skin="primary"> 
                <input type="checkbox" name="" title="写作" lay-skin="primary">
                <input type="checkbox" name="" title="发呆" lay-skin="primary"> 
                <input type="checkbox" name="" title="禁用" lay-skin="primary"> 
                <input type="checkbox" name="" title="写作" lay-skin="primary">
                <input type="checkbox" name="" title="发呆" lay-skin="primary"> 
                <input type="checkbox" name="" title="禁用" lay-skin="primary"> 
                <input type="checkbox" name="" title="写作" lay-skin="primary">
                <input type="checkbox" name="" title="发呆" lay-skin="primary"> 
                <input type="checkbox" name="" title="禁用" lay-skin="primary"> 
                <input type="checkbox" name="" title="写作" lay-skin="primary">
                <input type="checkbox" name="" title="发呆" lay-skin="primary"> 
                <input type="checkbox" name="" title="禁用" lay-skin="primary"> -->  
              </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo_hash">立即提交</button>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>

<script>
  layui.use(['form','route'], function () {

    var form = layui.form;
    var route = layui.route;
    var params = route.params();
    var groundData={}

    form.render();

    //页面获取
    DBHelper.Get('Admin_Xn_Page',function (res) {
      var father = $('.min_title')
      for(let i = 0 ; i < res.length ; i++){
        for(let j = 0 ; j < father.length ; j++){
          if(res[i].OneID == $(father[j]).attr('data-value')){
            $(father[j]).append('<input type="checkbox" class="page_list" name="page;'+res[i].Guid+'" title="'+res[i].Name+'" lay-skin="primary">')
          }
        }
      }
      form.render();

      //用户获取
      DBHelper.Get('Admin_Xn_User',function (res) {
        
        var userHtml = ''
        for(let i = 0 ; i < res.length ; i++){
          userHtml += '<input type="checkbox" class="user_list" name="user;'+res[i].Guid+'" title="'+res[i].RealName+'" lay-skin="primary">'
        }
        $('.user_area').html(userHtml)
        form.render();

        if(params!=null){//编辑

          //组基本信息
          DBHelper.Get('Admin_Xn_Group/'+params.guid,function (res) {
            groundData = res
            form.val('the_form',{
              Name:res.Name,
              ShortName:res.ShortName
            })
          },function (res) {
            // body...
          })

          //组关联页面
           DBHelper.Get('Admin_Xn_GroupPage?GroupGuid='+params.guid,function (res) {
             // console.log(res)
             let pageList = $('.page_list')
             for(let g = 0 ; g < res.length ; g++){
              res[g].PageGuid = 'page;'+res[g].PageGuid
              for(let t = 0 ; t < pageList.length ; t++) {
                if($((pageList)[t]).attr('name') == res[g].PageGuid){
                  $((pageList)[t]).attr('checked','checked')
                }
              }
             }
             form.render();
           },function (res) {
             // body...
           })

           //组关联用户
           DBHelper.Get('Admin_Xn_Group?guid='+params.guid,function (res) {
             console.log(res)
             let userList = $('.user_list')
             for(let g = 0 ; g < res.length ; g++){
              res[g].UserGuid = 'user;'+res[g].UserGuid
              for(let t = 0 ; t < userList.length ; t++) {
                if($((userList)[t]).attr('name') == res[g].UserGuid){
                  $((userList)[t]).attr('checked','checked')
                }
              }
             }
             form.render();
           },function (res) {
             // body...
           })
        }
      },function (res) {
        // body...
      })
    },function (res) {
      // body...
    })

    


    


    //监听提交
    form.on('submit(formDemo_hash)', function (data) {
      var a = Object.keys(data.field)
      var pageRegex = /^page;.*/
      var pageArr = []//页面数据
      var userRegex = /^user;.*/
      var userArr = []//用户数据
      for (let i = 0 ; i < a.length ; i++){
        if(pageRegex.test(a[i])){
          let b = a[i].split(';')
          b = b[1]
          pageArr.push(b)
        }
        if(userRegex.test(a[i])){
          let c = a[i].split(';')
          c = c[1]
          userArr.push(c)
        }
      }
      console.log(pageArr)
      console.log(userArr)


        if(params!=null){//是否编辑
          groundData.Name = data.field.Name
          groundData.ShortName = data.field.ShortName
          groundData.ModifyTime = getNowFormatDate()
          groundData.ModifyUser = localStorage.getItem('user'),
          DBHelper.Put('Admin_Xn_Group/'+params.guid,groundData,function (res) {
             DBHelper.DeleteSingle('Admin_Xn_GroupPage/'+params.guid,function (res) {
                

              },function (res) {
                DBHelper.DeleteSingle('Admin_Xn_GroupUser/'+params.guid,function (res) {
                  

                },function (res) {
                  for(let y = 0 ; y < pageArr.length ; y++){
                DBHelper.Post('Admin_Xn_GroupPage',{GroupGuid:params.guid,PageGuid:pageArr[y],CreateTime:getNowFormatDate(),CreateUser:localStorage.getItem('user'),ModifyTime:getNowFormatDate(),ModifyUser:localStorage.getItem('user'),IsDeleted:0},function (resa) {//用户组页面

              },function (resa) {
                // body...
              })
              }

              for(let k = 0 ; k < userArr.length ; k++){
                DBHelper.Post('Admin_Xn_GroupUser',{GroupGuid:params.guid,UserGuid:userArr[k],CreateTime:getNowFormatDate(),CreateUser:localStorage.getItem('user'),ModifyTime:getNowFormatDate(),ModifyUser:localStorage.getItem('user'),IsDeleted:0},function (resb) {
                  if(resb.GroupGuid){
                    layer.open({
                      content: '修改成功',
                      yes: function(index, layero){
                          location.href='#/layui/quanxianMana'
                          layer.close(index);
                      }
                    })
                  }
                },function (resb) {
                  // body...
                })
              }
                })
              })
          },function (res) {
            console.log(res)
          })
        }else {
          var newData = {
            Name:data.field.Name,
            ShortName:data.field.ShortName,
            CreateTime:getNowFormatDate(),
            CreateUser:localStorage.getItem('user'),
            ModifyTime:getNowFormatDate(),
            ModifyUser:localStorage.getItem('user'),
            IsDeleted:0
          }
          DBHelper.Post('Admin_Xn_Group',newData,function (res) {//用户组新增
            if (res.Guid) {

              
              for(let y = 0 ; y < pageArr.length ; y++){
                DBHelper.Post('Admin_Xn_GroupPage',{GroupGuid:res.Guid,PageGuid:pageArr[y],CreateTime:getNowFormatDate(),CreateUser:localStorage.getItem('user'),ModifyTime:getNowFormatDate(),ModifyUser:localStorage.getItem('user'),IsDeleted:0},function (resa) {//用户组页面

              },function (resa) {
                // body...
              })
              }

              for(let k = 0 ; k < userArr.length ; k++){
                DBHelper.Post('Admin_Xn_GroupUser',{GroupGuid:res.Guid,UserGuid:userArr[k],CreateTime:getNowFormatDate(),CreateUser:localStorage.getItem('user'),ModifyTime:getNowFormatDate(),ModifyUser:localStorage.getItem('user'),IsDeleted:0},function (resb) {
                  if(resb.GroupGuid){
                    layer.open({
                      content: '新建成功',
                      yes: function(index, layero){
                          location.href='#/layui/quanxianMana'
                          layer.close(index);
                      }
                    })
                  }
                },function (resb) {
                  // body...
                })
              }


            }
          },function (res) {
            // body...
          })
        }

      return false;
    });

  });
</script>
<style>
.layui-form-checkbox {
  display: block;
  width: 71px;
}
.layui-input, .layui-textarea {
  width: 98.5%;
}
.layui-form-label {
  width: 100px;
}
.layui-form-item .layui-form-checkbox[lay-skin=primary] {
  width: 150px;
}
.layui-form-checkbox[lay-skin=primary] span {
  width: 100px;
  text-align: left;
}
.min_title {
  color: #999;
  font-size: 14px;
  margin-top: 10px; 
}
</style>